package learn_0329;

import java.util.LinkedList;
import java.util.Queue;

/* 基本的⼆叉树节点 */
class TreeNode {
    int val;
    TreeNode left, right;
    public TreeNode(int val,TreeNode left,TreeNode right){
        this.val  = val;
        this.left = left;
        this.right = right;
    }
}
public class BfsLearn {
    static int minLength(TreeNode root){
        int depth = 0;
        if(root == null){
            return depth;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        depth = 0;
        int maxdepth = 0;

        while (!queue.isEmpty()){
            int qSize = queue.size();
            for(int i=0;i<qSize;i++){
                TreeNode cur = queue.poll();
//                if(cur.left == null && cur.right == null){
//                    return depth;
//                }
                if (cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
            }
            depth++;
            maxdepth = Math.max(maxdepth,depth);
        }
        return  depth;
    }

    public static void main(String[] args) {
        TreeNode one_left = new TreeNode(1,null,null );
        TreeNode one_right_left = new TreeNode(3,null,null );
        TreeNode one_right = new TreeNode(2,one_right_left,null );
        TreeNode root = new TreeNode(0,one_left,one_right);
        int depth = minLength(root);
        System.out.println(depth);
    }
}
